package com.itqn.servlet;

import com.itqn.pojo.Employee;
import com.itqn.service.EmployeeService;
import com.itqn.service.Impl.EmployeeServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/UpPasswordServlet")
public class UpPasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决post提交中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = response.getWriter();
        //获取参数
        String id = request.getParameter("id");
        String newPassword = request.getParameter("NewPassword");
        String oldPassword = request.getParameter("oldPassword");
        String confirm = request.getParameter("confirm");

        if (!confirm.equals(newPassword)) {
            writer.write("修改失败,确认密码错误!");
            return;
        } else {
            //创建实体类对象
            Employee employee=new Employee();
            employee.setId(Long.parseLong(id));
            employee.setPassword(newPassword);
            //创建业务逻辑层对象
            EmployeeService employeeService=new EmployeeServiceImpl();
            int result = employeeService.UpPassword(employee, oldPassword);
            switch (result) {
                case 0:
                    writer.write("修改失败,请重试!");
                    break;
                case 1:
                    writer.write("修改成功!");
                    break;
                case 2:
                    writer.write("修改失败,新密码与原始密码相同!");
                    break;
                case 3:
                    writer.write("修改失败,原始密码错误!");
                    break;
            }

        }

    }
}
